package com.huluobo.jetpackkotlindemo2006a.livedata_viewmodel

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.huluobo.jetpackkotlindemo2006a.R

/**
 * Created by lc on 2023/2/13.
 */
class StudentActivity : AppCompatActivity() {
    lateinit var studentViewModel: StudentViewModel
    lateinit var tvMessage: TextView
    lateinit var editText: EditText
    lateinit var button: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_student)

        tvMessage = findViewById(R.id.tvMessage)
        editText = findViewById(R.id.student_et)
        button = findViewById(R.id.btn)
        studentViewModel = ViewModelProvider(this).get(StudentViewModel::class.java)

//        val student = Student("LC", "123", 90)
//        studentViewModel.setStudentMessage(student)
//        studentViewModel._student.observe(this, {
//            tvMessage.text = "分数:" + it
//        })
//        button.setOnClickListener {
//            studentViewModel.getScore(editText.text.trim().toString()).observe(this, {
//                tvMessage.text = "分数:$it"
//            })
//        }
        studentViewModel.newScore.observe(this, Observer {
            tvMessage.text = "分数:$it"
        })

        button.setOnClickListener {
            studentViewModel.setStudentId(editText.text.trim().toString())
        }
    }
}